package com.hcrazy.manchester.app.controllers.seller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcrazy.manchester.app.pojo.BookCustomer;
import com.hcrazy.manchester.app.pojo.BookFood;
import com.hcrazy.manchester.app.pojo.BookMenu;
import com.hcrazy.manchester.app.pojo.BookOrder;
import com.hcrazy.manchester.app.pojo.ErrorMsg;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.pojo.Seller;
import com.hcrazy.manchester.app.pojo.User;
import com.hcrazy.manchester.app.services.BookFoodService;
import com.hcrazy.manchester.app.services.BookMenuService;
import com.hcrazy.manchester.app.services.BookOrderService;
import com.hcrazy.manchester.app.services.SellerService;
import com.hcrazy.manchester.app.utils.Constant;
import com.hcrazy.manchester.app.utils.Log;

@Controller
@RequestMapping("/api")
public class SellerAPIController {

	@Autowired
	private BookOrderService orderService;
	
	@Autowired
	private SellerService sellerService;
	
	@Autowired
	private BookMenuService menuService;
	
	@Autowired
	private BookFoodService foodService;
	
	
	/**
	 * 订餐
	 * @return
	 */
	@RequestMapping(value="/food/order",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object orderFood(
			HttpServletRequest req,@RequestBody BookOrder order){
		try {
			//如果用户未登录，返回错误码
			Object obj = req.getSession().getAttribute("user");
			if(null == obj){
				return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"会员未登录，不允许订餐！");
			}
			
			User user = (User)obj;
			long userId = user.getUserId();
			
			order.setUserId(userId);
			return orderService.addBookOrder(order);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	@RequestMapping(value="/book/customer",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object getUserInfo(HttpServletRequest req){
		try {
			//如果用户未登录，返回错误码
			Object obj = req.getSession().getAttribute("user");
			if(null == obj){
				return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"会员未登录！");
			}
			
			User user = (User)obj;
			long userId = user.getUserId();
			Object result = orderService.getCustomerByUserId(userId);
			if(null == result)
				result = new ErrorMsg(Constant.FAILED);
			return result;
			
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	
	}
	
	@RequestMapping(value="/book/customer",method=RequestMethod.POST)
	public @ResponseBody Object updateUserInfo(
			@RequestBody BookCustomer customer,
			HttpServletRequest req){
		try {
			//获取当前登录用户
			Object obj = req.getSession().getAttribute("user");
			if(null == obj){
				return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"会员未登录！");
			}
			User user = (User)obj;
			customer.setUserId(user.getUserId());
			
			orderService.addCustomer(customer);
			
			return setResult("true");
			
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	
	}
	
	private Map<String,String> setResult(String value){
		Map<String,String> result = new HashMap<String,String>();
		result.put(Constant.SINGLE_RETURN,value);
		return result;
	}
	
	
	/*******************************商家店铺******************************************************************/
	
	@RequestMapping(value="/book/seller",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody Object addSeller(@RequestBody Seller seller,
    		HttpServletRequest req,
    		Model model){
		Object object = req.getSession().getAttribute("user");
		if(null == object || !(object instanceof User)){
			return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"请首先登录系统");
		}
		User user = (User)object;
		seller.setUserId(user.getUserId());
		try {
			sellerService.addOrUpdateSeller(seller);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
		return setResult("true");
    }
	
	@RequestMapping(value="/book/sellers",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object getGroups(
			@RequestParam(required = false) Integer foodType,//所属专业，可选
			@RequestParam(required = false) String search,//搜索关键词，可选
			@RequestParam(required = false) Integer pageNum,//第几页
			@RequestParam(required = false) Integer numPerPage//一页请求多少条数据
			,HttpServletRequest req){
		PageModel pageModel = new PageModel();
		if(null != pageNum && pageNum > 0){
			pageModel.setPageNum(pageNum);
		}
		if(null != numPerPage && numPerPage > 0){
			pageModel.setNumPerPage(numPerPage);
		}
		if(null == foodType){
			foodType = 0;
		}
		if(null == search){
			search = "";
		}
		
		Object object = req.getSession().getAttribute("user");
		if(null == object || !(object instanceof User)){
			return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"请首先登录系统");
		}
		User user = (User)object;
		
		try {
			return sellerService.getSellers(pageModel, search, foodType, user.getUserId());
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	@RequestMapping(value="/book/seller",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object getSeller(
			@RequestParam long sellerId
			,HttpServletRequest req){
		
		Object object = req.getSession().getAttribute("user");
		if(null == object || !(object instanceof User)){
			return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"请首先登录系统");
		}
		
		try {
			return sellerService.getSellerById(sellerId);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	@RequestMapping(value="/book/seller/del/{sellerId}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object delSeller(
			@PathVariable("sellerId") long sellerId
			,HttpServletRequest req){
		
		Object object = req.getSession().getAttribute("user");
		if(null == object || !(object instanceof User)){
			return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"请首先登录系统");
		}
		
		try {
			sellerService.delSeller(sellerId);
			return setResult("true");
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	/*******************************菜单分类******************************************************************/
	
	@RequestMapping(value="/book/seller/menu",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody Object addMenu(@RequestBody BookMenu menu,
    		HttpServletRequest req,
    		Model model){
		Object object = req.getSession().getAttribute("user");
		if(null == object || !(object instanceof User)){
			return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"请首先登录系统");
		}
		
		try {
			menuService.addOrUpdateBookMenu(menu);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
		return setResult("true");
    }
	
	@RequestMapping(value="/book/seller/menus",produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object getAllBookMenus(@RequestParam long sellerId){
		try {
			return menuService.getAllMenus(Constant.TYPE_BOOK_FOOD_MENU, sellerId);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	@RequestMapping(value="/book/seller/menu/del/{menuId}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object delMenu(
			@PathVariable("menuId") int menuId
			,HttpServletRequest req){
		
		Object object = req.getSession().getAttribute("user");
		if(null == object || !(object instanceof User)){
			return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"请首先登录系统");
		}
		
		try {
			menuService.delBookMenu(menuId);
			return setResult("true");
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	/*******************************菜品信息******************************************************************/
	
	@RequestMapping(value="/book/seller/food",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody Object addFood(@RequestBody BookFood food,
    		HttpServletRequest req,
    		Model model){
		Object object = req.getSession().getAttribute("user");
		if(null == object || !(object instanceof User)){
			return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"请首先登录系统");
		}
		
		User user = (User)object;
		food.setUserId(user.getUserId());
		
		try {
			foodService.addOrUpdateBookFood(food);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
		return setResult("true");
    }
	
	@RequestMapping(value="/book/seller/foods",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object getFoods(
			@RequestParam(required = false) Long sellerId,//所属专业，可选
			@RequestParam(required = false) Integer menuId,//所属专业，可选
			@RequestParam(required = false) String search,//菜品名称
			@RequestParam(required = false) Integer pageNum,//第几页
			@RequestParam(required = false) Integer numPerPage//一页请求多少条数据
			,HttpServletRequest req){
		PageModel pageModel = new PageModel();
		if(null != pageNum && pageNum > 0){
			pageModel.setPageNum(pageNum);
		}
		if(null != numPerPage && numPerPage > 0){
			pageModel.setNumPerPage(numPerPage);
		}
		if(null == sellerId){
			sellerId = 0L;
		}
		if(null == menuId){
			menuId = 0;
		}
		if(null == search){
			search = "";
		}
		
		Object object = req.getSession().getAttribute("user");
		if(null == object || !(object instanceof User)){
			return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"请首先登录系统");
		}
		User user = (User)object;
		
		try {
			return foodService.getBookFoods(pageModel, sellerId, menuId, search, user.getUserId(),Constant.TYPE_BOOK_FOOD);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	@RequestMapping(value="/book/seller/food/del/{foodId}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object delFood(
			@PathVariable("foodId") int foodId
			,HttpServletRequest req){
		
		Object object = req.getSession().getAttribute("user");
		if(null == object || !(object instanceof User)){
			return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"请首先登录系统");
		}
		
		try {
			foodService.delBookFood(foodId);
			return setResult("true");
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	/*******************************客户信息******************************************************************/
	
	@RequestMapping(value="/book/seller/customers",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object getCustomers(
			@RequestParam(required = false) Long sellerId,//所属专业，可选
			@RequestParam(required = false) String search,//菜品名称
			@RequestParam(required = false) Integer pageNum,//第几页
			@RequestParam(required = false) Integer numPerPage//一页请求多少条数据
			,HttpServletRequest req){
		PageModel pageModel = new PageModel();
		if(null != pageNum && pageNum > 0){
			pageModel.setPageNum(pageNum);
		}
		if(null != numPerPage && numPerPage > 0){
			pageModel.setNumPerPage(numPerPage);
		}
		if(null == sellerId){
			sellerId = 0L;
		}
		
		if(null == search){
			search = "";
		}
		
		Object object = req.getSession().getAttribute("user");
		if(null == object || !(object instanceof User)){
			return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"请首先登录系统");
		}
		User user = (User)object;
		
		try {
			return orderService.getPagedCustomers(pageModel, search, user.getUserId(),sellerId,Constant.TYPE_BOOK_FOOD);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
/*******************************订单信息******************************************************************/
	
	@RequestMapping(value="/book/seller/orders",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object getOrders(
			@RequestParam(required = false) Long sellerId,//所属专业，可选
			@RequestParam(required = false) String search,//菜品名称
			@RequestParam(required = false) String orderDateStart,//菜品名称
			@RequestParam(required = false) String orderDateEnd,//菜品名称
			@RequestParam(required = false) Integer pageNum,//第几页
			@RequestParam(required = false) Integer status,//所属专业，可选
			@RequestParam(required = false) Integer numPerPage//一页请求多少条数据
			,HttpServletRequest req){
		
		PageModel pageModel = new PageModel();
		if(null != pageNum && pageNum > 0){
			pageModel.setPageNum(pageNum);
		}
		if(null != numPerPage && numPerPage > 0){
			pageModel.setNumPerPage(numPerPage);
		}
		if(null == sellerId){
			sellerId = 0L;
		}
		
		if(null == search){
			search = "";
		}
		
		if(null == status){
			status = Constant.TYPE_BOOK_ORDER_ALL;
		}
		
		if(null == orderDateStart || null == orderDateEnd){
			orderDateStart = "";
			orderDateEnd = "";
		}
		
		Object object = req.getSession().getAttribute("user");
		if(null == object || !(object instanceof User)){
			return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"请首先登录系统");
		}
		User user = (User)object;
		
		try {
			return orderService.getPagedOrders(pageModel, search, user.getUserId(), sellerId, orderDateStart, orderDateEnd, status,Constant.TYPE_BOOK_FOOD);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	@RequestMapping(value="/book/seller/order",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody Object updateOrder(
    		@RequestParam long orderId,
    		@RequestParam Integer status,
    		HttpServletRequest req,
    		Model model){
		Object object = req.getSession().getAttribute("user");
		if(null == object || !(object instanceof User)){
			return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"请首先登录系统");
		}
		
		try {
			BookOrder order = new BookOrder();
			order.setOrderId(orderId);
			order.setStatus(status);
			orderService.updateBookOrder(order);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
		return setResult("true");
    }
	
	/*************************************客户端功能****************************************************/
	
	@RequestMapping(value="/sellers",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object getAllSellers(
			@RequestParam(required = false) Integer foodType,//所属专业，可选
			@RequestParam(required = false) String search,//搜索关键词，可选
			@RequestParam(required = false) Integer pageNum,//第几页
			@RequestParam(required = false) Integer numPerPage//一页请求多少条数据
			,HttpServletRequest req){
		PageModel pageModel = new PageModel();
		if(null != pageNum && pageNum > 0){
			pageModel.setPageNum(pageNum);
		}
		if(null != numPerPage && numPerPage > 0){
			pageModel.setNumPerPage(numPerPage);
		}
		if(null == foodType){
			foodType = 0;
		}
		if(null == search){
			search = "";
		}
		
		Object object = req.getSession().getAttribute("user");
		if(null == object || !(object instanceof User)){
			return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"请首先登录系统");
		}
		
		try {
			return sellerService.getSellers(pageModel, search, foodType, 0);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	@RequestMapping(value="/book/orders",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object getMyOrders(
			@RequestParam(required = false) Long sellerId,//所属专业，可选
			@RequestParam(required = false) String search,//菜品名称
			@RequestParam(required = false) String orderDateStart,//菜品名称
			@RequestParam(required = false) String orderDateEnd,//菜品名称
			@RequestParam(required = false) Integer pageNum,//第几页
			@RequestParam(required = false) Integer status,//所属专业，可选
			@RequestParam(required = false) Integer numPerPage//一页请求多少条数据
			,HttpServletRequest req){
		
		PageModel pageModel = new PageModel();
		if(null != pageNum && pageNum > 0){
			pageModel.setPageNum(pageNum);
		}
		if(null != numPerPage && numPerPage > 0){
			pageModel.setNumPerPage(numPerPage);
		}
		if(null == sellerId){
			sellerId = 0L;
		}
		
		if(null == search){
			search = "";
		}
		
		if(null == status){
			status = Constant.TYPE_BOOK_ORDER_ALL;
		}
		
		if(null == orderDateStart || null == orderDateEnd){
			orderDateStart = "";
			orderDateEnd = "";
		}
		
		Object object = req.getSession().getAttribute("user");
		if(null == object || !(object instanceof User)){
			return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"请首先登录系统");
		}
		User user = (User)object;
		
		try {
			return orderService.getUserPagedOrders(pageModel, search, user.getUserId(), sellerId, orderDateStart, orderDateEnd, status,Constant.TYPE_BOOK_FOOD);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
}
